<?php
/**
 * @file individualdonorv1.php
 * Creates UI for managing individual donors. Allows user to view all
 * individuals in the database, or to view by the first letter of a donor's
 * last name.
 * @author Matt Vartanian
 * @version 10/2/2011
 */
include ('header.php');
include ('../db-control/dbIndividual.php');
include ('../db-control/dbDonor.php');

individualDonor( );

/**
 * Performs function calls to display header and create list of letters for
 * donor last name viewing.
 */
function individualDonor( )
{
        individualDonorHeader( );
        displaySearchLetters( );
        if(isset($_GET['search'])) {
                displayButtons( );
        }
        if(isset($_GET['search']) && $_GET['search'] == "all"){
                displayAllIndividuals( );
        }
        if(isset($_GET['search']) && $_GET['search'] != "all"){
                displayIndividualsByLetter( );
        }
        if(isset($_POST['deleteIndividual']) && isset($_POST['individualId'])) {
                deleteIndividual( );
        }
        if(isset($_POST['deleteIndividual']) && !isset($_POST['individualId'])) {
                echo "No individuals were selected";
        }
}

/**
 * Retrieves all users from database and creates array of objects corresponding
 * to each row in the table
 */
function displayIndividualsByLetter( )
{
        $letterChosen = $_GET['search'];
        $lastNamesArray = dbIndividual::getIndividualsByLastName($letterChosen);
        $objectArray = array();
        $objectArray = convertToObjectArray($lastNamesArray);
        selectionBox($objectArray);
}

/**
 * Displays all individuals
 * @param $objectArray array of all individuals in database
 */
function displayAllIndividuals( )
{
        $objectArray = array ( );
        $allIndArray = array( );
        dbIndividual::getAllIndividuals($allIndArray);
        if($allIndArray == "") {
                echo "No individual donors were found. To add a new donor, click
                <a href=\"addIndDonor.php\">here</a>";
        } else {
                $objectArray = convertToObjectArray($allIndArray);
        }
        selectionBox($objectArray);
}

/**
 * Deletes individuals that are selected
 */
function deleteIndividual( )
{
        $indArray = array( );
        $objectArray = array( );
        $deleteConfirmationString = "";
        $toBeDeleted = $_POST['individualId'];

        foreach($toBeDeleted as $item) {//creates array of individuals given ids
                $indArray[ ]= dbIndividual::getIndividualById($item);
        }
        $objectArray = convertToObjectArray($indArray);
        //checks if id matches, if it does, deletes individual from database
        foreach($toBeDeleted as $item) {
                foreach($objectArray as $object){
                        if ($object->getDonorID() == $item) {
                                dbDonor::deleteDonor($item);
                                $deleteConfirmationString .= $object->getLastName() .", ".
                                $object->getFirstName(). "<br />";
                        }
                }
        }
        echo "The following individuals have been removed from the database: <br/>
        $deleteConfirmationString";

}

/**
 * Creates array of objects with firstName, lastName, and id
 * @param $individualArray array resource returned by sql query
 */
function convertToObjectArray($individualArray)
{
        $objectArray = array( );
        foreach($individualArray as $item) { // creates array of objects
                $objectArray[ ] = new Individual($item['donorID'],
                $item['firstName'],
                $item['lastName'] );
        }
        return $objectArray;
}

/**
 * Creates selection boxes next to names
 */
function selectionBox($objectArray)
{
        foreach($objectArray as $object){
                echo <<<_END
         <input type="checkbox" name="individualId[]" value="{$object->getDonorID( )}" />
         <a href = "addIndDonor.php?id={$object->getDonorID()}">
         {$object->getLastName( )},  {$object->getFirstName( )}</a><br />
_END;
        }
        echo "</form>";
}

/**
 * Creates add, edit, and delete buttons.
 */
function displayButtons( )
{
        echo <<<_END
                <form method = "post" action="individualdonorv1.php">
                <input type= "submit" value="Delete Selected" name="deleteIndividual" />
                <br />
_END;
}

/**
 * Creates alphabetic links
 */
function displaySearchLetters( )
{
        echo "Individual Donors - <a href = \"addIndDonor.php\">Add New</a><br /><br />";
        echo "To search by last name:  ";
        for($i=65; $i<91; $i++){
                if($i == 65){
                        echo "<a href=\"http://localhost/donate/view-control/individualdonorv1.php?search=all\">All</a>"."- ";
                }
                echo "<a href=\"http://localhost/donate/view-control/individualdonorv1.php?search=".chr($i)."\">".chr($i)."</a>"."   ";
        }
        echo "<hr/>";
}

